package cn.aitrox.ry.service.oss.service.impl;

import cn.aitrox.ry.bean.RespResult;
import cn.aitrox.ry.common.exception.CustomBizException;
import cn.aitrox.ry.common.service.BaseService;
import cn.aitrox.ry.enumtype.RespCodeEnum;
import cn.aitrox.ry.service.oss.dto.FileRestoreInfoDto;
import cn.aitrox.ry.service.oss.enumtype.StorageTypeEnum;
import cn.aitrox.ry.service.oss.sdk.LocalOssSdk;
import cn.aitrox.ry.service.oss.sdk.OssSdk;
import cn.aitrox.ry.service.oss.sdk.UcloudOssSdk;
import cn.aitrox.ry.service.oss.service.OssFileService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.InputStream;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class OssFileServiceImpl extends BaseService implements OssFileService {

    @Autowired
    private UcloudOssSdk ucloudSdk;

    @Autowired
    private LocalOssSdk localOssSdk;


    @Override
    public List<FileRestoreInfoDto> urlGet(List<String> resourceKeys, Integer expireSeconds, String ossType) {
        OssSdk ossSdk = getOssSdk(ossType);
        return resourceKeys.stream().map(x -> {
            RespResult<FileRestoreInfoDto> respResult = ossSdk.getDownloadUrl(x, expireSeconds);
            if (!respResult._isOk()) {
                throw new CustomBizException(respResult.getMsg());
            }
            return respResult.getData();
        }).collect(Collectors.toList());
    }

    @Override
    public RespResult put(String key, InputStream inputStream, String originalFilename, String ossType) {
        OssSdk ossSdk = getOssSdk(ossType);
        return ossSdk.putFileByIs(key, inputStream, originalFilename);
    }

    @Override
    public RespResult putLocal(String key, String filePath, String ossType) {
        OssSdk ossSdk = getOssSdk(ossType);
        return ossSdk.putFile(key, new File(filePath));
    }

    @Override
    public RespResult delete(String key, String ossType) {
        return getOssSdk(ossType).deleteFile(key);
    }

    @Override
    public RespResult copy(String sourceKey, String targetKey, String ossType) {
        return getOssSdk(ossType).copyFile(sourceKey, targetKey);
    }

    @Override
    public RespResult move(String filePath, String key, String ossType) {
        File file = new File(filePath);
        if (!file.exists()) {
            return RespResult.error(RespCodeEnum.FILE_NOT_EXISTED);
        }
        return getOssSdk(ossType).moveFile(key, filePath);
    }

    private OssSdk getOssSdk(String ossType) {
        if (StringUtils.isEmpty(ossType)) {
            return localOssSdk;
        }

        StorageTypeEnum typeEnum = StorageTypeEnum.getEnum(ossType);

        if (null == typeEnum) {
            throw new CustomBizException("不支持的存储类型");
        }

        switch (typeEnum) {
            case LOCAL:
                return localOssSdk;
            case UCLOUD:
                return ucloudSdk;
            default:
                throw new CustomBizException("不支持的存储类型");
        }

    }
}
